package top.aigoo.project01.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 判断验证码是否符合预期
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/8/13 21:01
 */
public class CodeUtil {

    /**
     * 校验验证码
     */
    public static boolean checkVerifyCode(HttpServletRequest request) {
        //从session中取出来系统生成的验证码
        String verifyCodeExpected = (String) request.getSession().getAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        // 从HttpServletRequest中接收前端发送过来的验证码
        String verifyCodeActual = HttpServletRequestUtil.getString(request, "verifyCodeActual");

        if (verifyCodeActual == null
                || !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) {
            return false;
        }
        return true;
    }
}
